from tkinter import *

class App:
    
    def __init__(self, master, notes, x = 90, level = 0):
        '''
        Creates the sheet music for the melody of our song
        if the not is a sharp or flat it is purple
        if not it is black
        '''
        self.x=x #just some starting variable used latter for notes
        self.notes=notes
        
        self.frameConor = Frame(master)   #creates the frame
        self.frameConor.pack()
        
        def find_height(notes):  #a function to find out how many clefs we need
            num_notes = (len(notes[0]))
            done = False
            while not done:
                if num_notes%27 == 0:
                    done = True
                else:
                    num_notes += 1
            return (num_notes/27)*200
            
        self.canvas = Canvas(self.frameConor, width = 1000, height = (find_height(notes) + 20)) #creates a canvas
        self.canvas.pack()
         
        def make_line(level): #a function to make each clef decided y based on the level
            self.canvas.create_line (100,(45+level),950,45+level)
            self.canvas.create_line (100,(55+level),950,55+level) 
            self.canvas.create_line (100,(65+level),950,65+level) 
            self.canvas.create_line (100,(75+level),950,75+level) 
            self.canvas.create_line (100,(85+level),950,85+level)  
        
            photo_g = PhotoImage(file = 'G_Clef.gif') #photo of the g clef
            self.gClef = Label(image=photo_g)
            self.gClef.image = photo_g
            self.file = "G_Clef.gif"
            self.gClef.remember=photo_g
            self.canvas.create_image(110,(67+level),image = photo_g)
        
            self.canvas.create_line (100,(145+level),950,145+level)
            self.canvas.create_line (100,(155+level),950,155+level) 
            self.canvas.create_line (100,(165+level),950,165+level) 
            self.canvas.create_line (100,(175+level),950,175+level) 
            self.canvas.create_line (100,(185+level),950,185+level)
            
            photo_f = PhotoImage(file = 'F_Clef.gif') #photo of the g clef
            self.fClef = Label(image=photo_f)
            self.fClef.image = photo_f
            self.file = "F_Clef.gif"
            self.fClef.remember=photo_f
            self.canvas.create_image(60,(167+level),image = photo_f)
              
        def is_even(note): # find if the MID note is even used to see if it is a sharp or flat
            if note % 2 == 0:
                return True
            else:
                return False
            
        make_line(level) # makes the first level
            
        for positions in range(len(notes)): #writes all the noted
            x += 25
            for a_note in notes[positions]:
                if x < 900:  #will go until it runs out of room
                    
                    if a_note >= 65: #These are to deal with the fact e sharp is not a note and b #
                        a_note += 1
                    if a_note >= 73:
                        a_note += 1
                    if a_note >= 79:
                        a_note += 1
                    if a_note >= 87:
                        a_note += 1
                    if a_note >= 93:
                        a_note += 1
                    if a_note >= 101:
                        a_note += 1
                    if a_note >= 107:
                        a_note += 1
                    if a_note >= 115:
                        a_note += 1
                    if a_note >= 121:
                        a_note += 1
                    if a_note >= 129:
                        a_note += 1
                    if a_note >= 135:
                        a_note += 1
                    
                    if a_note <= 59:
                        a_note -= 1
                    if a_note <= 51:
                        a_note -= 1
                    if a_note <= 45:
                        a_note -= 1
                    if a_note <= 37:
                        a_note -= 1
                    if a_note <= 31:
                        a_note -= 1
                    if a_note <= 23:
                        a_note -= 1
                    if a_note <= 17:
                        a_note -= 1
                    if a_note <= 9:
                        a_note -= 1
                    if a_note <= 3:
                        a_note -= 1
                    if a_note <= -5:
                        a_note -= 1
                    
                    if a_note > 59: 
                        print (a_note)   
                        if is_even(a_note): #for regular notes
                            y_1 = 90 - (2.5*((a_note)-60)) + level
                            y_2 = y_1 + 10
                            self.canvas.create_oval(x,y_1,(x+20),y_2, fill='black')
                        else: #for sharps and flats
                            y_1 = 90 - (2.5*((a_note-1)-60)) + level  
                            y_2 = y_1 + 10
                            self.canvas.create_oval(x,y_1,(x+20),y_2, fill='black')
                        
                            photo_s = PhotoImage(file = 'Sharp.gif') #photo of the sharp
                            self.sharp = Label(image=photo_s)
                            self.sharp.image = photo_s
                            self.file = "Sharp.gif"
                            self.sharp.remember=photo_s
                            self.canvas.create_image((x-1),(y_1-5),image = photo_s)           
                    
                    else:
                        print (a_note)
                        if is_even(a_note): #for regular notes
                            y_1 = 130 - (2.5*((a_note)-60)) + level
                            y_2 = y_1 + 10
                            self.canvas.create_oval(x,y_1,(x+20),y_2, fill='black')
                        else: #for sharps and flats
                            y_1 = 130 - (2.5*((a_note-1)-60)) + level  
                            y_2 = y_1 + 10
                            self.canvas.create_oval(x,y_1,(x+20),y_2, fill='black')
                        
                            photo_s = PhotoImage(file = 'Sharp.gif') #photo of the sharp
                            self.sharp = Label(image=photo_s)
                            self.sharp.image = photo_s
                            self.file = "Sharp.gif"
                            self.sharp.remember=photo_s
                            self.canvas.create_image((x-1),(y_1-5),image = photo_s)
                    x += 30
                    make_line(level) # this makes sure the lines are on top
                    
                else:  # will create a new level and a new clef, also plots the first note
                    x = 115
                    level += 200
                    make_line(level)
                    
                    if a_note >= 65: #These are to deal with the fact e sharp is not a note and b #
                        a_note += 1
                    if a_note >= 73:
                        a_note += 1
                    if a_note >= 79:
                        a_note += 1
                    if a_note >= 87:
                        a_note += 1
                    if a_note >= 93:
                        a_note += 1
                    if a_note >= 101:
                        a_note += 1
                    if a_note >= 107:
                        a_note += 1
                    if a_note >= 115:
                        a_note += 1
                    if a_note >= 121:
                        a_note += 1
                    if a_note >= 129:
                        a_note += 1
                    if a_note >= 135:
                        a_note += 1
                    
                    if a_note <= 59:
                        a_note -= 1
                    if a_note <= 51:
                        a_note -= 1
                    if a_note <= 45:
                        a_note -= 1
                    if a_note <= 37:
                        a_note -= 1
                    if a_note <= 31:
                        a_note -= 1
                    if a_note <= 23:
                        a_note -= 1
                    if a_note <= 17:
                        a_note -= 1
                    if a_note <= 9:
                        a_note -= 1
                    if a_note <= 3:
                        a_note -= 1
                    if a_note <= -5:
                        a_note -= 1
                        
                    if a_note > 59: 
                        print (a_note)   
                        if is_even(a_note): #for regular notes
                            y_1 = 90 - (2.5*((a_note)-60)) + level
                            y_2 = y_1 + 10
                            self.canvas.create_oval(x,y_1,(x+20),y_2, fill='black')
                        else: #for sharps and flats
                            y_1 = 90 - (2.5*((a_note-1)-60)) + level  
                            y_2 = y_1 + 10
                            self.canvas.create_oval(x,y_1,(x+20),y_2, fill='black')
                        
                            photo_s = PhotoImage(file = 'Sharp.gif') #photo of the sharp
                            self.sharp = Label(image=photo_s)
                            self.sharp.image = photo_s
                            self.file = "Sharp.gif"
                            self.sharp.remember=photo_s
                            self.canvas.create_image((x-1),(y_1-5),image = photo_s)           
                    
                    else:
                        print (a_note)
                        if is_even(a_note): #for regular notes
                            y_1 = 130 - (2.5*((a_note)-60)) + level
                            y_2 = y_1 + 10
                            self.canvas.create_oval(x,y_1,(x+20),y_2, fill='black')
                        else: #for sharps and flats
                            y_1 = 130 - (2.5*((a_note-1)-60)) + level  
                            y_2 = y_1 + 10
                            self.canvas.create_oval(x,y_1,(x+20),y_2, fill='black')
                        
                            photo_s = PhotoImage(file = 'Sharp.gif') #photo of the sharp
                            self.sharp = Label(image=photo_s)
                            self.sharp.image = photo_s
                            self.file = "Sharp.gif"
                            self.sharp.remember=photo_s
                            self.canvas.create_image((x-1),(y_1-5),image = photo_s)  
                    
                    make_line(level) # this makes sure the lines are on top              
                    x += 30
            
        self.button = Button(self.frameConor, text="QUIT", fg="red", command=self.frameConor.quit) #just a little quite button
        self.button.pack(side = BOTTOM)
        
def makeSheet(noteList=[[80,79,78,77,76,75,74,73,72,71,70,69,68,67,66,65,64,63,62,61,60,59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,41,40]]):
              
              #,58,67,68,56,71,65,63,66,66,66,66,66,66,66,66,67,67,61,54,60,59,58,67,68,56,71,65,63,66,66,66,66,66,66,66,66,67,67,61,54,60,59,58,67,68,56,71,65,63,66,66,66,66,66,66,66,66,67,67]]):
    '''
    used mostly for testing since did not have parters code the entire time
    just writes the tkinter
    '''
    something = Tk()
    something.wm_title('Sheet Music')
    app = App(something, noteList)
    something.mainloop()
    
makeSheet()